﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zuJianClient.PacketDto;

namespace zuJianClient.Utils
{
    public static class PubHelper
    {
        /// <summary>
        /// 该方法接受一个字符串和一个编码作为参数，并返回该字符串的十六进制表示形式
        /// </summary>
        /// <param name="input"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string ConvertStringToHex(string input, Encoding encoding)
        {
            byte[] bytes = encoding.GetBytes(input);
            return BitConverter.ToString(bytes).Replace("-", "");
        }

        /// <summary>
        /// 字符串转换为字节数组
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static byte[] StringToByteArray(string hex)
        {
            int numberChars = hex.Length;
            byte[] bytes = new byte[numberChars / 2];
            for (int i = 0; i < numberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }

        /// <summary>
        /// 高低位的转换,返回数字
        /// </summary>
        /// <param name="HexStr">十六进制字符串</param>
        /// <returns></returns>
        public static int HightLowConver(string HexStr)
        {
            int intLength = HexStr.Length;
            string res = string.Empty;
            for (int i = 0; i < intLength / 2; i++)
            {
                res += HexStr.Substring(intLength - 2 * (i + 1), 2);
            }
            return Convert.ToInt32(res, 16);
        }

        /// <summary>
        /// 高低位的转换,返回数字
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <returns></returns>
        public static int HightLowConver(byte[] bytes)
        {
            string res = string.Empty;
            for (int i = 0; i < bytes.Length / 2; i++)
            {
                res += bytes[bytes.Length - 2 * (i + 1)].ToString("X2");
                res += bytes[bytes.Length - 2 * (i + 1) + 1].ToString("X2");
            }
            return Convert.ToInt32(res, 16);
        }


        /// <summary>
        /// 高低位的转换,返回十六进制字符串
        /// </summary>
        /// <param name="number">数字</param>
        /// <returns></returns>
        public static string HightLowConver(int number, string format)
        {
            string str = number.ToString(format).ToLower();
            int intLength = str.Length;
            string res = string.Empty;
            for (int i = 0; i < intLength / 2; i++)
            {
                res += str.Substring(intLength - 2 * (i + 1), 2);
            }
            return res;
        }

        /// <summary>
        /// 计算校验和
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static byte CalculateChecksum(string content)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(content);
            byte checksum = 0;
            foreach (byte b in bytes)
            {
                checksum ^= b;
            }
            return checksum;
        }

        /// <summary>
        /// 用于确保数组的长度符合预期，并在需要时在前面填充额外的空字节
        /// </summary>
        /// <param name="array"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static byte[] EnsureLength(byte[] array, int length)
        {
            if (array.Length < length)
            {
                // 如果数组长度不足，填充额外的空字节
                byte[] newArray = new byte[length];
                Array.Copy(array, 0, newArray, length - array.Length, array.Length);
                return newArray;
            }
            else if (array.Length > length)
            {
                // 如果数组长度超过，截断至指定长度
                byte[] newArray = new byte[length];
                Array.Copy(array, array.Length - length, newArray, 0, length);
                return newArray;
            }
            else
            {
                // 数组长度符合预期，无需调整
                return array;
            }
        }
    }
}
